<template>
  <s-layout
    title="门店列表"
    navbar="custom"
    :showLeftButton="true"
    navbarMode="normal"
    :bgStyle="template.style?.background"
    :navbarStyle="template.style?.navbar"
    onShareAppMessage
    :showFloatButton="true"
  >
    <view class="shop-page">
      <!-- 搜索区域 -->
      <view class="search-section">
        <view class="search-container">
          <view class="search-box">
            <uni-icons type="search" size="18" color="#999"></uni-icons>
            <input
              class="search-input"
              placeholder="搜索门店名称或地址"
              v-model="searchKeyword"
              @input="onSearch"
            />
          </view>
        </view>
      </view>

      <!-- 门店列表 -->
      <scroll-view
        class="shop-list"
        scroll-y="true"
        @scrolltolower="loadMore"
        :scroll-top="scrollTop"
      >
        <view
          class="shop-item"
          v-for="(shop, index) in shopList"
          :key="shop.id"
          @click="goToShopDetail(shop)"
        >
          <!-- 左侧图片区域 -->
          <view class="shop-image-container">
            <image
              class="shop-image"
              :src="shop.logo || shop.image"
              mode="aspectFill"
            />
          </view>

          <!-- 右侧信息区域 -->
          <view class="shop-info">
            <view class="shop-header">
              <view class="shop-name">{{ shop.shopName || shop.name }}</view>
              <view class="shop-distance" v-if="shop.distance">
                <text class="distance-text">{{ shop.distance }}</text>
              </view>
            </view>

            <view class="shop-address">
              <uni-icons
                type="location-filled"
                size="14"
                color="#ff6b35"
              ></uni-icons>
              <text class="address-text">{{ shop.address }}</text>
            </view>

            <view class="shop-time" v-if="shop.businessHours">
              <uni-icons type="clock" size="14" color="#666"></uni-icons>
              <text class="time-text">营业时间：{{ shop.businessHours }}</text>
            </view>

            <view class="shop-contact" v-if="shop.phone">
              <uni-icons type="phone" size="14" color="#ff6b35"></uni-icons>
              <text class="contact-text">{{ shop.phone }}</text>
            </view>

            <!-- 支持电子券标签 -->
            <!-- <view class="shop-tags">
              <view class="tag">支持电子券</view>
            </view> -->
          </view>
        </view>

        <!-- 加载更多提示 -->
        <view class="load-more" v-if="loading">
          <text class="load-text">加载中...</text>
        </view>

        <view class="load-more" v-if="!hasMore && shopList.length > 0">
          <text class="load-text">没有更多数据了</text>
        </view>
      </scroll-view>

      <!-- 空状态 -->
      <view class="empty-state" v-if="shopList.length === 0 && !loading">
        <image
          class="empty-image"
          src="/static/images/empty-shop.png"
          mode="aspectFit"
        />
        <text class="empty-text">暂无门店信息</text>
      </view>
    </view>
  </s-layout>
</template>

<script>
import sheep from "@/sheep";

export default {
  data() {
    return {
      searchKeyword: "",
      shopList: [], // 门店列表
      page: {
        pageNum: 1,
        pageSize: 10,
        pageTotal: 0,
      },
      longitude: "", // 经度
      latitude: "", // 纬度
      loading: false, // 加载状态
      hasMore: true, // 是否还有更多数据
      scrollTop: 0,
    };
  },
  onLoad() {
    console.log("门店列表页面加载");
    this.initData();
  },
  computed: {
    template() {
      return sheep.$store("app").template.user;
    },
  },
  methods: {
    // 初始化数据
    async initData() {
      await this.getLocation();
      this.getShopList();
    },

    // 获取用户位置
    getLocation() {
      return new Promise((resolve, reject) => {
        uni.getLocation({
          type: "wgs84",
          success: (res) => {
            this.longitude = res.longitude;
            this.latitude = res.latitude;
            console.log("当前位置的经度：" + res.longitude);
            console.log("当前位置的纬度：" + res.latitude);
            resolve(res);
          },
          fail: (err) => {
            console.error("获取位置失败：", err);
            resolve(null); // 即使失败也resolve，避免阻塞后续流程
          },
        });
      });
    },

    // 搜索
    onSearch() {
      // 重置分页
      this.page.pageNum = 1;
      this.shopList = [];
      this.hasMore = true;
      // 延迟搜索，避免频繁请求
      clearTimeout(this.searchTimer);
      this.searchTimer = setTimeout(() => {
        this.getShopList();
      }, 500);
    },

    // 加载更多
    loadMore() {
      if (this.loading || !this.hasMore) {
        return;
      }
      this.page.pageNum++;
      this.getShopList();
    },

    // 获取门店列表
    async getShopList() {
      if (this.loading) return;
      this.loading = true;

      try {
        const params = {
          address: this.searchKeyword, // 搜索关键词作为地址参数
          page: this.page.pageNum,
          size: this.page.pageSize,
          latitude: this.latitude,
          longitude: this.longitude,
        };

        const res = await sheep.$api.cake.getStoreList(params);
        if (res.code === 200) {
          const newData = res.data.content || [];

          if (this.page.pageNum === 1) {
            // 首次加载或搜索时重置数据
            this.shopList = newData;
          } else {
            // 翻页时追加数据
            this.shopList = this.shopList.concat(newData);
          }

          this.page.pageTotal = res.data.totalPages;

          // 判断是否还有更多数据
          this.hasMore = this.page.pageNum < this.page.pageTotal;

          // 为每个门店添加状态信息（如果接口没有返回的话）
          this.shopList = this.shopList.map((shop) => {
            if (!shop.status) {
              shop.status = "open";
              shop.statusText = "营业中";
            }
            return shop;
          });
        }
      } catch (error) {
        console.error("获取门店列表失败：", error);
        uni.showToast({
          title: "获取门店列表失败",
          icon: "none",
        });
      } finally {
        this.loading = false;
      }
    },

    // 跳转门店详情 - 修改为跳转到cakelist页面
    goToShopDetail(shop) {
      // 跳转到cakelist页面，传递门店ID
      sheep.$router.go(`/pages/cake/cakelist?storeId=${shop.managerId}`);
    },
  },
};
</script>

<style lang="scss" scoped>
.shop-page {
  background-color: #f5f5f5;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

// 搜索区域
.search-section {
  background: #fff;
  padding: 30rpx;
  border-bottom: 1rpx solid #f0f0f0;
  flex-shrink: 0;
  padding-top: 200rpx;
}

.search-container {
  display: flex;
  align-items: center;
}

.search-box {
  flex: 1;
  display: flex;
  align-items: center;
  background: #f8f8f8;
  border-radius: 50rpx;
  padding: 20rpx 30rpx;
}

.search-input {
  flex: 1;
  margin-left: 20rpx;
  font-size: 28rpx;
  color: #333;

  &::placeholder {
    color: #999;
  }
}

// 门店列表 - 修改为左右布局
.shop-list {
  flex: 1;
  padding: 20rpx;
  box-sizing: border-box;
}

.shop-item {
  background: #fff;
  border-radius: 20rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  display: flex; // 改为flex布局
  align-items: flex-start;
}

// 左侧图片容器
.shop-image-container {
  width: 120rpx;
  height: 120rpx;
  border-radius: 16rpx;
  overflow: hidden;
  margin-right: 24rpx;
  flex-shrink: 0;
}

.shop-image {
  width: 100%;
  height: 100%;
}

// 右侧信息区域
.shop-info {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.shop-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 12rpx;
}

.shop-name {
  font-size: 32rpx;
  font-weight: 600;
  color: #333;
  flex: 1;
  line-height: 1.3;
}

.shop-distance {
  margin-left: 20rpx;
}

.distance-text {
  font-size: 24rpx;
  color: #999;
  background: #f5f5f5;
  padding: 4rpx 12rpx;
  border-radius: 12rpx;
}

.shop-address,
.shop-time,
.shop-contact {
  display: flex;
  align-items: center;
  margin-bottom: 8rpx;
}

.address-text {
  font-size: 26rpx;
  color: #666;
  margin-left: 8rpx;
  line-height: 1.4;
  flex: 1;
}

.time-text {
  font-size: 24rpx;
  color: #666;
  margin-left: 8rpx;
}

.contact-text {
  font-size: 26rpx;
  color: #ff6b35;
  margin-left: 8rpx;
  font-weight: 500;
}

// 标签区域
.shop-tags {
  margin-top: 12rpx;
}

.tag {
  display: inline-block;
  background: #e3f2fd;
  color: #1976d2;
  font-size: 22rpx;
  padding: 6rpx 12rpx;
  border-radius: 12rpx;
  border: 1rpx solid #bbdefb;
}

// 加载更多
.load-more {
  display: flex;
  justify-content: center;
  padding: 30rpx;
}

.load-text {
  font-size: 26rpx;
  color: #999;
}

// 空状态
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 40rpx;
  flex: 1;
}

.empty-image {
  width: 200rpx;
  height: 200rpx;
  margin-bottom: 30rpx;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
}
</style>
